package com.lanou.dllo.findfood.adpter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.AppResult;
import com.lanou.dllo.findfood.tools.UpdateManger;
import com.lanou.dllo.findfood.util.AppdownListener;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.List;

/**
 * Created by dllo on 15/11/11.
 */
public class AppAboutAdpter extends
        RecyclerView.Adapter<AppAboutAdpter.RecylerAdpter>{

    private List<AppResult>results;
    private AppdownListener listener;

    public void setListener(AppdownListener l) {
        this.listener = l;
    }

    public void  addData(List<AppResult>result){
        results = result;
        notifyDataSetChanged();
    }
    @Override
    public RecylerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.app_about_item,null);
        return new RecylerAdpter(view);
    }

    @Override
    public void onBindViewHolder(RecylerAdpter holder, int position) {
        holder.position = position;
        AppResult result = results.get(position);
        if (result!= null){
          getBitmap(result.getIcon(), holder.icon);
            holder.appDescrition.setText(result.getDescription());
            holder.appName.setText(result.getName());

        }


    }
    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }

    @Override
    public int getItemCount() {
        return results != null && results.size() > 0 ? results.size():0;
    }



    class RecylerAdpter  extends  RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView icon;
        private TextView appName,appDescrition;
        private Button down;
        private int position;


        public RecylerAdpter(View itemView) {
            super(itemView);
            icon = (NetworkImageView) itemView.findViewById(R.id.appIcon);
            appName = (TextView) itemView.findViewById(R.id.appName);
            appDescrition = (TextView) itemView.findViewById(R.id.appDescrition);
            down = (Button) itemView.findViewById(R.id.down);
            down.setOnClickListener(this);


        }
        private UpdateManger mUpdateManager;
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.down://点击下载按钮的实现方法
                    AppResult result = results.get(position);
                    if (listener != null){
                        listener.appdown(result);
                    }
                    break;
                default:
                    break;
            }
        }
    }
//    public  void  addData( List<AppResult> lists){
//        results = lists;
//        notifyDataSetChanged();
//    }

}
